//
//  V4Home01View.m
//  XinKaiFa55like
//
//  Created by 55like on 22/09/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "V4Home01View.h"
#import "RHTableView.h"
#import "V4TestLicensePlateContentViewController.h"
#import "MyUiIAlertView.h"
#import "V4TestLicensePlateContentViewController.h"

@interface V4Home01View ()<UITableViewDelegate,UITableViewDataSource>
{
    
    
    
}
@property(nonatomic,strong)RHTableView*mtableView;

@end

@implementation V4Home01View
#pragma mark  开始
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self addView];
        [self request];
    }

    return self;
}
#pragma mark -  写UI
-(void)addView{
    _mtableView =[[RHTableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    
    _mtableView.delegate = self;
    _mtableView.dataSource = self;
    _mtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_mtableView showRefresh:YES LoadMore:YES];
    [self addSubview:_mtableView];
    
    
//    _mtableView.dataArray=[NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"",@""]];
//    [_mtableView reloadData];
    
    
    
}


#pragma mark  tableview网络请求
-(void)request{
    krequestParam
//    app/products/mycollection   uid=470&token=e5f1a35a3621eac0d123dafa7a212823&cid=&keys=&page=1&pagesize=20&levelid=
    [dictparam setObject:@"%@" forKey:@"page"];
    [dictparam setObject:@"20" forKey:@"pagesize"];
    if (_cid) {
        [dictparam setObject:_cid forKey:@"cid"];
    }
    
    _mtableView.urlString=[NSString stringWithFormat:@"products/mycollection%@",dictparam.wgetParamStr];
    [_mtableView refresh];
    
    
}
#pragma mark - 按钮事件监听


#pragma mark - 代理事件



#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(RHTableView *)tableView{
    
    
    return 1;
    
}

-(NSInteger)tableView:(RHTableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return tableView.dataArray.count;
}

-(CGFloat)tableView:(RHTableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 94+4.5;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    return 0.001;
}

-(UIView *)tableView:(RHTableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
    
    
}

#pragma mark  cell处理
- (UITableViewCell *)tableView:(RHTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView ==_mtableView){
        //
        UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            cell.frameWidth=kScreenWidth;
            cell.backgroundColor=zrgbwhiteColor;
            UILabel*lbtitle=[RHMethods lableX:10 Y:16 W:kScreenWidth-20 Height:14 font:14 superview:cell withColor:nil text:@"上海梧桐融资租赁有限公司（简称梧桐公司）"];
            UILabel*lbleft1=[RHMethods lableX:25 Y:lbtitle.frameYH+ 15 W:kScreenWidth/2-25-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"售价：30万  "];
            UILabel*lbleft2=[RHMethods lableX:25 Y:lbleft1.frameYH+10   W:kScreenWidth/2-25-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"成立时间：2017-1-1"];
            UILabel*lbright1=[RHMethods lableX:kScreenWidth/2 Y:lbleft1.frameY W:kScreenWidth/2-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"注册资本：300万"];
            UILabel*lbright2=[RHMethods lableX:kScreenWidth/2 Y:lbleft2.frameY W:kScreenWidth/2-10 Height:12 font:12 superview:cell withColor:rgb(102,102,102) text:@"发布时间：2017-6-1"];
            UIView*viewlin=[RHMethods viewWithFrame:CGRectMake(0, 93.5, kScreenWidth, 5) backgroundcolor:rgbGray superView:cell];
            tableView.backgroundColor=rgbGray;
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            [cell setAddValue:lbleft1 forKey:@"lbleft1"];
            [cell setAddValue:lbleft2 forKey:@"lbleft2"];
            [cell setAddValue:lbright1 forKey:@"lbright1"];
            [cell setAddValue:lbright2 forKey:@"lbright2"];
            [cell setAddValue:viewlin forKey:@"viewlin"];
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            
            
        }
        UILabel*lbtitle=[cell getAddValueForKey:@"lbtitle"];
        UILabel*lbleft1=[cell getAddValueForKey:@"lbleft1"];
        UILabel*lbleft2=[cell getAddValueForKey:@"lbleft2"];
        UILabel*lbright1=[cell getAddValueForKey:@"lbright1"];
        UILabel*lbright2=[cell getAddValueForKey:@"lbright2"];
        
                NSDictionary *dic=tableView.dataArray[indexPath.row];
        lbtitle.text=[dic objectForJSONKey:@"title"];
        lbleft1.text=[NSString stringWithFormat:@"售价：%@",[dic objectForJSONKey:@"price"]];
        lbright1.text=[NSString stringWithFormat:@"注册资本：%@",[dic objectForJSONKey:@"regcapital"]];
        lbleft2.text=[NSString stringWithFormat:@"成立时间：%@",[dic objectForJSONKey:@"regtime"]];
        lbright2.text=[NSString stringWithFormat:@"发布时间：%@",[dic objectForJSONKey:@"ctime"]];
        
        return cell;
        
    }
    return [UITableViewCell new];
}
#pragma mark  选中cell
-(void)tableView:(RHTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic=tableView.dataArray[indexPath.row];
      [UTILITY.currentViewController pushController:[V4TestLicensePlateContentViewController class] withInfo:@"Insurance" withTitle:@"牌照详情" withOther:dic];
}

- (NSArray *)tableView:(RHTableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 添加一个删除按钮
    
    __weak __typeof(self) weakSelf = self;
    NSDictionary *dic=tableView.dataArray[indexPath.row];
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        //        NSLog(@"点击了删除");
        
        [MyUiIAlertView showAlerttWithTitle:@"是否删除此条记录?" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定" WithAlertBlock:^(UIAlertView * _Nonnull alertView, NSInteger buttonIndex) {
            if (buttonIndex==1) {
                krequestParam
                [dictparam setObject:@"0" forKey:@"act"];
                
                [dictparam setObject:[dic objectForJSONKey:@"id"] forKey:@"id"];
                [NetEngine createPostAction:@"homes/collection" withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
                    if ([[resData getSafeObjWithkey:@"status"] isEqualToString:@"200"]) {
                        [tableView.dataArray removeObject:dic];
                        [tableView reloadData];
                        [SVProgressHUD showSuccessWithStatus:[resData valueForJSONKey:@"info"]];
                        //            [self addView];
                        
                    }else{
                        [SVProgressHUD showErrorWithStatus:[resData valueForJSONKey:@"info"]];
                        
                    }
                }];
                
            }
        }];
        
    }];
    
    return @[deleteRowAction];
}
@end
